package com.quectel.core.module.carparking.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.carparking.dao.CarParkingDoorDao;
import com.quectel.core.module.carparking.dto.CarParkingDoorDto;
import com.quectel.core.module.carparking.entity.CarParkingDoorEntity;
import com.quectel.core.module.carparking.service.CarParkingDoorService;
import com.quectel.core.module.carparking.service.CarParkingService;
import com.quectel.core.module.device.dto.DeviceDto;
import com.quectel.core.module.device.service.DeviceService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author bobo
 * @email bob.yu@quectel.com
 * @date 2022/02/22 09:44
 */
@DubboService
public class CarParkingDoorServiceImpl implements CarParkingDoorService {

    private static final Logger LOGGER = LoggerFactory.getLogger(CarParkingDoorServiceImpl.class);

    private static final String CAR_PARKING_DOOR_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "CAR_PARKING_DOOR_CACHE_BY_ID:";

    @Autowired
    private CarParkingDoorDao carParkingDoorDao;
    @Autowired
    private VillageService villageService;
    @Autowired
    private CarParkingService carParkingService;
    @Autowired
    private DeviceService deviceService;

    @Override
    public CarParkingDoorDto selectOne(Long deviceId) {
        CarParkingDoorEntity entity = carParkingDoorDao.selectOne(
                new LambdaQueryWrapper<CarParkingDoorEntity>()
                        .eq(CarParkingDoorEntity::getDeviceId, deviceId)
                        .last(SqlConstants.MYSQL_LIMIT_ONE)
        );
        return paddingField(CopyUtils.copyObj(entity, CarParkingDoorDto.class));
    }

    @Override
    public List<CarParkingDoorDto> selectByVillageId(Long villageId) {
        List<CarParkingDoorEntity> entityList = carParkingDoorDao.selectList(new LambdaQueryWrapper<CarParkingDoorEntity>()
                .eq(CarParkingDoorEntity::getVillageId, villageId)
        );
        return CopyUtils.copyList(entityList, CarParkingDoorDto.class);
    }

    @Override
    public CarParkingDoorDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                CAR_PARKING_DOOR_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public CarParkingDoorDto selectById(Long id) {
        CarParkingDoorEntity entity = carParkingDoorDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, CarParkingDoorDto.class));
    }

    @Override
    public List<CarParkingDoorDto> queryList(Map<String, Object> params) {
        List<CarParkingDoorEntity> list = carParkingDoorDao.queryList(params);
        List<CarParkingDoorDto> result = CopyUtils.copyList(list, CarParkingDoorDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private CarParkingDoorDto paddingField(CarParkingDoorDto dto) {
        if (dto != null) {
            if (dto.getVillageId() != null) {
                VillageDto village = villageService.selectCacheById(dto.getVillageId());
                if (village != null) {
                    dto.setVillageName(village.getName());
                }
            }
            if (dto.getDeviceId() != null) {
                DeviceDto deviceDto = deviceService.selectCacheById(dto.getDeviceId());
                if (deviceDto != null) {
                    dto.setDevice(deviceDto);
                }
            }

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return carParkingDoorDao.queryTotal(params);
    }

    @Override
    public Long save(CarParkingDoorDto dto) {
        CarParkingDoorEntity entity = CopyUtils.copyObj(dto, CarParkingDoorEntity.class);
        entity.setId(Snowflake.nextId());
        carParkingDoorDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(CarParkingDoorDto dto) {
        CarParkingDoorEntity entity = CopyUtils.copyObj(dto, CarParkingDoorEntity.class);
        carParkingDoorDao.updateById(entity);
        CacheKit.invalidRedisCache(CAR_PARKING_DOOR_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(CarParkingDoorDto dto) {
        CarParkingDoorEntity entity = CopyUtils.copyObj(dto, CarParkingDoorEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, carParkingDoorDao);

        CacheKit.invalidRedisCache(CAR_PARKING_DOOR_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        carParkingDoorDao.deleteById(id);

        CacheKit.invalidRedisCache(CAR_PARKING_DOOR_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
